package com.unknow.first.dapp.admin.account.job;

import com.unknow.first.dapp.admin.account.service.PayLogsService;
import lombok.extern.slf4j.Slf4j;
import org.cloud.core.redis.RedisUtil;
import org.cloud.scheduler.constants.MisfireEnum;
import org.cloud.scheduler.job.BaseQuartzJobBean;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.Assert;


/**
 * 扫描日志
 */
@Slf4j
public class Erc20PayJob extends BaseQuartzJobBean {

    final private String redisLockerName = "AddressAccount.Erc20PayJob.locker";
    RedisUtil redisUtil;
    PayLogsService payLogsService;

    public Erc20PayJob(RedisUtil redisUtil, PayLogsService payLogsService) {
        this.redisUtil = redisUtil;
        this.payLogsService = payLogsService;
    }


    @Override
    protected void init() {
        this.jobName = "ERC20线上冲值定时任务";
        jobData.put("description", "ERC20线上冲值定时任务");
        this.jobTime = "0/5 * * * * ? ";
        setMisfire(MisfireEnum.CronScheduleMisfireEnum.MISFIRE_INSTRUCTION_DO_NOTHING);
    }

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Assert.isTrue(redisUtil != null, "redisutil 未获取到");
        if (!redisUtil.getLock(redisLockerName, 60 * 60 * 1000)) {
            return;
        }
        try {
            payLogsService.processPay();
        } catch (Exception e) {
            log.error("{}", e);
            e.printStackTrace();
        } finally {
            redisUtil.releaseLock(redisLockerName);
        }
    }
}